﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

public partial class _Gallery : System.Web.UI.Page 
{
    protected LocalFunctions FUNC;
    private int iTotalPages = 0;
    private int iPageSize = 2;
    public int CurrentPage = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Rondo)Master).FUNC;
        sendto.FUNC = FUNC;

        lnkGallery.Attributes.Add("style", (FUNC.LangID == 1) ? FUNC.sSelected : FUNC.sSelectedHeb);
        GetParameters();

        Dal.arParams.Clear();
        Dal.arParams.Add(7);
        Dal.arParams.Add(FUNC.LangID);
        DataTable DTPage = Dal.ExecuteDBsp("RD_SelectStaticPageById", Dal.arParams);
        if (DTPage != null)
        {
            if (DTPage.Rows.Count > 0)
            {
                Title = DTPage.Rows[0]["PageTitle"].ToString();
                imgTitle.Src = FUNC.FullAppPath + "/Gallery/" + DTPage.Rows[0]["imageLink"].ToString();
            }
        }

        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID);
        Dal.arParams.Add(CurrentPage);
        Dal.arParams.Add(iPageSize);
        Dal.arParams.Add(0); //only image galeries
        DataTable DT= Dal.ExecuteDBsp("RD_SelectAllGalleries", Dal.arParams);
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                iTotalPages = (int)Math.Ceiling((double)Convert.ToInt32(DT.Rows[0]["TotalRows"]) / iPageSize);
                dlGallery.DataSource = DT;
                dlGallery.DataBind();
            }
        SimplePager();
    }

    private void GetParameters()
    {
        CurrentPage = Convert.ToInt32(Request.QueryString["page"]);
        CurrentPage = (CurrentPage == 0) ? 1 : CurrentPage;
    }

    protected void dlGallery_DataBind(object sender, DataListItemEventArgs e)
    {
        int iGaleryID=Convert.ToInt32(((HiddenField)e.Item.FindControl("hfGalID")).Value);
        DataList dlImages = ((DataList)e.Item.FindControl("rptGalTopImages"));

        if (FUNC.LangID == 2)
        {
            HtmlTable tblMorePics = ((HtmlTable)e.Item.FindControl("tblMorePics"));
            tblMorePics.Attributes.Add("dir", "rtl");
        }
        Dal.arParams.Clear();
        Dal.arParams.Add(iGaleryID);

        DataTable DTImages = Dal.ExecuteDBsp("RD_SelectTopImages", Dal.arParams);
        if (DTImages != null)
            if (DTImages.Rows.Count > 0)
            {
                dlImages.DataSource = DTImages;
                dlImages.DataBind();
            }
    }

    public void SimplePager()
    {
        if (CurrentPage < 0) CurrentPage = 1;
        if (iTotalPages < 0) iTotalPages = 0;

        var pager = new PagerBuilder();

        

        var start = Math.Max(CurrentPage - 2, 1);
        var end = Math.Min(iTotalPages, CurrentPage + 2);

        for (var i = start; i <= end; i++)
        {
            if (i == CurrentPage)
                pager.AddPage(i.ToString(), i, "font-weight: bold; color: White;");
            else
                pager.AddPage(i.ToString(), i);
        }
        
        if (FUNC.LangID == 2)
        {
            pager.Sort();
        }

        if (CurrentPage > 1)
        {
            pager.InsertPage(0,"&lt;&lt;", 1);
            pager.InsertPage(1,"&lt;", CurrentPage - 1);
        }

        if (CurrentPage < iTotalPages)
        {
            pager.AddPage("&gt;", CurrentPage + 1);
            pager.AddPage("&gt;&gt;", iTotalPages);
        }
        
        divPager.InnerHtml += pager.ToString();
    }
}

public class PagerBuilder
{
    private class PagerLink
    {
        public string Title { get; set; }
        public int PageNo { get; set; }
        public string Class { get; set; }
    }

    private readonly List<PagerLink> _pagerLinks =
            new List<PagerLink>();

    public PagerBuilder()
    {
             
    }

    public void Sort()
    {
        _pagerLinks.Sort(delegate(PagerLink x, PagerLink y) { return x.PageNo.ToString().CompareTo(y.PageNo.ToString()) * -1; });
    }

    public string PagerClass { get; set; }

    public void AddPage(string title, int pageNo)
    {
        AddPage(title, pageNo, string.Empty);
    }

    public void InsertPage(int iPlace,string title, int pageNo)
    {
        InsertPage(iPlace,title, pageNo, string.Empty);
    }

    public void AddPage(string title, int pageNo,
                        string itemClass)
    {
        var link = new PagerLink
        {
            PageNo = pageNo,
            Title = title,
            Class = itemClass
        };
        _pagerLinks.Add(link);
    }

    public void InsertPage(int iPalce,string title, int pageNo,
                           string itemClass)
    {
        var link = new PagerLink
        {
            PageNo = pageNo,
            Title = title,
            Class = itemClass
        };
        _pagerLinks.Insert(iPalce,link);
    }   

    public override string ToString()
    {
        var builder = new StringBuilder();
        foreach (var link in _pagerLinks)
        {
            builder.Append("<a");
            if (link.Class != string.Empty)
            {
                builder.AppendFormat(" style=\"{0}\"", link.Class);
            }
            builder.Append(" href=\"");
            builder.AppendFormat("gallery.aspx?page={0}", link.PageNo);
            builder.Append("\">");
            builder.Append(link.Title);
            builder.Append("</a>&nbsp;");
        }
        return builder.ToString();
    }

    
    

}
